#########################################################################################
# Title: Appendix_C.R
# Author: Yuki Atsusaka (atsusaka@rice.edu)
# Aim: Code to replicate results in Online Appendix C of Atsusaka (2021)
#########################################################################################

# CLEAN THE GLOBAL ENVIRONMENT AND READ PACKAGES
rm(list=ls())
library(scales)
library(graphics)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# FUNCTION TO PREDICT THE PROBABILITY OF MINORITY CANDIDATE EMERGENCE AND VICTORY
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

minorep <- function(M, C, gap=NULL){

# TURNOUT ADJUSTED PERCENTAGE OF MINORITY VOTERS
if(is.null(gap)){
C = C
}else{
C = (C*gap[1])/(C*gap[1] + (100-C)*gap[2])*100
}

q = sqrt(M*C) - 50 # GEOMETRIC MEAN
p = pnorm(q=q,  mean=0, sd=1)
  
return(p)
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# FUNCTION TO SIMULATE THE RACIAL MARGIN OF VICTORY
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

M.sim <- function(C, coethnic, crossover){
   
V_m = C*coethnic + ((100-C)*crossover)
V_w = C*(1-coethnic) + ((100-C)*(1-crossover))
M = 1/2*(V_m - V_w) + 50

return(M)   
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# FUNCTION TO PREDICT THE IMPACT OF REDISTRICTING
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

redistrict <- function(coethnic, crossover, gap=NULL){

# PERCENTAGE OF MINORITY VOTERS
C = seq(from=1, to=100, by=0.1) 
    
# TURNOUT ADJUSTED PERCENTAGE OF MINORITY VOTERS
if(is.null(gap)){
C = C
}else{
C = (C*gap[1])/(C*gap[1] + (100-C)*gap[2])*100
}
  
# SIMULATED RACIAL MARGIN OF VICTORY
V_m = C*coethnic + ((100-C)*crossover)
V_w = C*(1-coethnic) + ((100-C)*(1-crossover))
M = 1/2*(V_m - V_w) + 50 

# MODEL PREDICTIONS
MC.sqrt.min50 = sqrt(C*M) - 50  # GEOMETRIC MEAN  
out <- pnorm(MC.sqrt.min50)     # Probability of Candidate Emergence

return(out)
}

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# FUNCTION TO SPREDICT THE NUMBER OF MINORITY WINNERS
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#

n.minorep <- function(out){

# MONTE CARLO SIMULATION
N_pred <- NA

for(i in 1:1000){
model.sample = sapply(out, function(x) rbinom(n=1, size=1, prob=x))
N_pred[i] <- sum(model.sample)  
} # CLOSE THE LOOP

return(N_pred)
}

############################################################### 
# Figure C1: Predicting the Probability of Minority Electoral Success
###############################################################

M <- seq(from=0, to=100, by=0.01) # Adjusted Racial Margin of Victory
C <- rep(x=40, times=length(M))   # Percentage of Minority Voters (SET TO 25)

pred1 <- minorep(M=M, C=C)
pred2 <- minorep(M=M, C=C, gap=c(0.5,0.4))
pred3 <- minorep(M=M, C=C, gap=c(0.4,0.5))
M.prime <- M.sim(C=40, coethnic=1, crossover=0.3)


pdf(here::here("Figure_C1.pdf"), height=5, width=5)
par(mar=c(4,4,3,1))
plot(pred1 ~ M,
     type="n",
     xlim=c(50,100),
     ylab="Pr(Minoriy Electoral Success)",
     xlab="M (Raical Margin of Victory)",
     main="Pr(Minority Win) in 40% Minority Districts",
     mgp=c(2,0.7,0), cex.lab=1.2)
rect(40, 0.5, 120, 1.2, col=alpha("gray80", 0.3), lty=0)
lines(pred1 ~ M, col="maroon", lwd=4)
lines(pred2 ~ M, col="dimgray", lwd=4, lty=3)
lines(pred3 ~ M, col="seagreen", lwd=4, lty=2)
abline(v=M.prime, col=alpha("dimgray", 0.3), lwd=2)
text(x=51, y=1, labels="M'", col="dimgray", font=2)
arrows(x0=52.5, x1=57.5, y0=1,y1=1, length=0.1, col="dimgray")
legend(x=75,y=0.45, lty=c(3,1,2), 
       col=c("dimgray", "maroon", "seagreen"),
       lwd=1.5, box.lty=0, cex=0.9, bty="n",
       text.font=2, text.col="dimgray",
       title="Turnout Rates by Race",
       legend=c("minority 0.5 \nwhite 0.4",
                "no turnout gap",
                "minority 0.4 \nwhite 0.5"))
dev.off()

######################################################################## 
# Figure C2: Simulating the Impact of Redistricting on Minority Representation
########################################################################

C <- seq(from=1, to=100, by=0.1)
start <- 59.2
end <- 63.2
mid <- (start+end)/2
pred <- redistrict(coethnic=1, crossover=0, gap=c(0.5, 0.6)) 


pdf(here::here("Figure_C2.pdf"), height=5, width=5)
par(mar=c(4,4,3,1))
plot(0, type="n", ylim=c(-0.1,1.1),xlim=c(start-5,end+5),
   ylab="Pr(Minority Electoral Success)",xlab="C (% of Minority Voters)",
   mgp=c(2,0.7,0), cex.lab=1.2)
lines(pred ~ C, col="maroon",lwd=4)
rect(35, -0.2, start, 1.2, col=alpha("gray80", 0.3), lty=0)
rect(end, -0.2, 100, 1.2, col=alpha("gray80", 0.3), lty=0)
points(x=start, y=pred[C==start], pch=1, cex=2, col=scales::alpha("maroon",0.9))
points(x=end, y=pred[C==end], pch=16, cex=2, col=scales::alpha("maroon",0.9))
text(x=start, y=pred[C==start]-0.09, labels=round(pred[C==start],d=3), col="maroon")
text(x=end+1, y=pred[C==end]-0.09, labels=round(pred[C==end],d=3), col="maroon")
arrows(x0=start, x1=mid,y0=1,y1=1, length=0.1, lwd=4, col="maroon")
title("Senate (59.2%) v. Alternative (63.2%) Plan")
text(x=58,y=0.4,
     labels="Pr(minority bloc) = 1\nPr(crossover) = 0\nMinority turnout = 0.5\nWhite turnout = 0.6",
     col="dimgray", font=2)
dev.off()

 

######################################################################## 
# Figure C3: Finding Sufficient Percentage of Minority Voters and Sweet Spot
########################################################################

pred <- redistrict(coethnic=1, crossover=0.3, gap=c(0.4, 0.5))
assure <- 1     # Assure
equal.op <- 0.5 # Equal Opportunity
C <- seq(from=1, to=100, by=0.1)

pdf(here::here("Figure_C3.pdf"), height=5, width=5)
par(mar=c(4,4,3,1))
plot(0, type="n", ylim=c(-0.1,1.1),xlim=c(40,70),
   ylab="Pr(Minority Electoral Success)",xlab="C (% of Minority Voters)",
   mgp=c(2,0.7,0), cex.lab=1.2)
rect(sw1, -0.2, sw2, 1.2, col=alpha("gray80", 0.3), lty=0)
lines(pred ~ C, col="maroon",lwd=4)
abline(h=assure,   lty=2, col="dimgray")
abline(h=equal.op, lty=2, col="dimgray")
text(x=64.2,y=assure+0.05,   labels="Assuring minority rep", font=2, cex=1, col="dimgray")
text(x=63.2,y=equal.op+0.05, labels="Equal opportunity",     font=2, cex=1, col="dimgray")

sw1 = min(C[pred>=assure])   # SWEET SPLOT based on Assure
sw2 = min(C[pred>=equal.op]) # SWEET SPLOT based on Equal Opportunity

arrows(x0=sw1, x1=sw1,
       y0=assure, y1=-0.05, col="maroon", lwd=1, length=0.1, lty=1)
arrows(x0=sw2, x1=sw2,
       y0=equal.op, y1=-0.05, col="maroon", lwd=1, length=0.1)
points(x=sw1, y=assure, cex=1.5, pch=16, col="maroon")
points(x=sw2, y=equal.op, cex=1.5, pch=16, col="maroon")
text(x=sw1+0.3, y=-0.1, labels=paste0(sw1, "%"), col="maroon")
text(x=sw2+0.3, y=-0.1, labels=paste0(sw2, "%"), col="maroon")

text(x=64, y=0.2, 
     labels="Pr(minority bloc) = 1\nPr(crossover) = 0.3\nMinority turnout = 0.4\nWhite turnout = 0.5",
     cex=0.9, col="dimgray", font=2)
title("Assuring v. Equal Opportunity")
dev.off()

######################################################################## 
# Figure C4: Predicting the Number of Minority Officeholders
########################################################################

M <- c(30,40,50,65,70,30)
M.ave <- sqrt(max(M)*min(M)) # Geometric Mean
M.ave <- rep(M.ave, times=5)
C.atlarge <- rep(47.5, times=5)
C.smd <- c(50,40,60,30,50,80)

pred.atlarge <- minorep(M=M.ave, C=C.atlarge)
pred.smd <- minorep(M=M, C=C.smd)

n.atlarge <- n.minorep(pred.atlarge)
n.smg <- n.minorep(pred.smd)


pdf(here::here("Figure_C4.pdf"), height=4, width=8)
par(mfrow=c(1,2), mar=c(4,4,3,1))   
hist(n.atlarge,
     ylab="Samples from Monte Carlo Simulations",
     xlab="Predicted Number of Minority Officeholders",
     border=F,
     col="gray40",
     xlim=c(0,3),
     cex.lab=1.2,
     main="At-Large Election")   
   
hist(n.smg,
     ylab="Samples from Monte Carlo Simulations",
     xlab="Predicted Number of Minority Officeholders",
     border=F,
     col="gray40",
     xlim=c(0,3),
     cex.lab=1.2,     
     main="Single-Member District")
dev.off()


#########################################################################################
# END OF THIS R SOURCE FILE
#########################################################################################